|
|
Component
FreescaleCAN
CAN communication for Freescale implementation
Component Level: High
Category:
CPU Internal Peripherals-Communication
Typical Usage:
(Examples of a typical usage of the component in user code.
For more information please
see the page Component Code
Typical Usage.)
Typical settings and usage of FreescaleCAN component. Required component name is "CAN"
(1) Acceptance code filtering
MAIN.C
void main(void)
{
//Sets the code of accepted message.
CAN_SetAcceptanceCode(0x15263740);
//Set the acceptance mask to 3, i.e. messages with IDs
//0x15263740,0x15263741,0x15263742 and 0x15263743 are accepted
CAN_SetAcceptanceMask(0x00000003);
//...
//All messages are accepted
CAN_SetAcceptanceMask(0xFFFFFFFF);
//...
}
(2) Communication without using interrupts (polling mode).
MAIN.C
byte buff[]={1,2,3,4,5,6,7,8};
void main(void)
{
for(;;) {
//Send data frame via buffer 0 with ID=0x512.
//The buffer consists of 8 bytes.
CAN_SendFrame(0, 0x512, DATA_FRAME, 8, buff);
}
}
(3) Standard and Extended identifier sending
MAIN.C
byte buff[]={1,2,3,4,5,6,7,8};
dword messageID = 0x512;
void main(void)
{
//Send data frame via buffer 0 with extended ID=0x512.
CAN_SendFrame(0,(messageID|CAN_EXTENDED_FRAME_ID),DATA_FRAME,8,buff);
//Send data frame via buffer 0 with standart ID=0x512.
CAN_SendFrame(0,messageID, DATA_FRAME, 8, buff);
}
(4) Communication with interrupts and using communication events.
EVENTS.C
void CAN1_OnFullRxBuffer(void)
{
dword ID;
byte tupe, len;
byte buff[];
word bufferMask;
//Test what message buffer have received data.
//Only for HW CAN implementations with more than one receive buffer
if (CAN1_GetStateRX() & CAN_MB0_MASK) {
//Read received data in message buffer 0 configured as receive
//Note: the first parameter "BufferNum" in method ReadFrame
//is valid for FlexCAN modules only
//if they have more than one receive buffer
CAN_ReadFrame(0, &ID, &type, &len, buff);
//Send data back via message buffer 1 configured as transmit
CAN_SendFrame(1, ID, type, len, buff);
}
}
|